﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.IO;

namespace testVideo
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {
        string extension;

        Cad.CAD proc = new Cad.CAD();
        static int WIDTH = 100;
        static int HEIGHT = 100;


        public MainWindow()
        {
            InitializeComponent();
        }

        private void fenHoloXia_Loaded(object sender, RoutedEventArgs e)
        {
            ProjetHologramme.HoloXiaDataSet holoXiaDataSet = ((ProjetHologramme.HoloXiaDataSet)(this.FindResource("HoloXiaDataSet")));
            // Chargez les données dans la table File. Vous pouvez modifier ce code si nécessaire.
            ProjetHologramme.HoloXiaDataSetTableAdapters.FileTableAdapter holoXiaDataSetFileTableAdapter = new ProjetHologramme.HoloXiaDataSetTableAdapters.FileTableAdapter();
            holoXiaDataSetFileTableAdapter.Fill(holoXiaDataSet.File);
            System.Windows.Data.CollectionViewSource fileViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("fileViewSource")));
            fileViewSource.View.MoveCurrentToFirst();
        }

        

        public void affichageDonnee()
        {

            string path = proc.SelectByName(nameComboBox.Text);
            extension = Path.GetExtension(path);
            if (extension != null)
            {
                if (extension == ".png" || extension == ".jpg")
                {
                    List<Image> listImages = new List<Image>();
                    listImages = creationImag();

                    int[,] tableauPosition = new int[4, 3]
                                            {{0, 0, 1},
                                         {1,  1, 2},
                                         {2,  2, 1},
                                         {3,  1, 0}};

                    gridHolo.HorizontalAlignment = HorizontalAlignment.Center;
                    gridHolo.VerticalAlignment = VerticalAlignment.Center;
                    //gridHolo.ShowGridLines = true;

                    for (int i = 0; i < 3; i++)
                    {

                        gridHolo.RowDefinitions.Add(new RowDefinition());
                        gridHolo.ColumnDefinitions.Add(new ColumnDefinition());
                    }

                    for (int i = 0; i < listImages.Count; i++)
                    {
                        Grid.SetRow(listImages[tableauPosition[i, 0]], tableauPosition[i, 1]);
                        Grid.SetColumn(listImages[tableauPosition[i, 0]], tableauPosition[i, 2]);
                        gridHolo.Children.Add(listImages[tableauPosition[i, 0]]);
                    }

                }
                else if (extension == ".mp4")
                {
                    List<MediaElement> listMedia = new List<MediaElement>();
                    listMedia = creatMedia();

                    int[,] tableauPosition = new int[4, 3]
                                            {{0, 0, 1},
                                         {1,  1, 2},
                                         {2,  2, 1},
                                         {3,  1, 0}};

                    gridHolo.HorizontalAlignment = HorizontalAlignment.Center;
                    gridHolo.VerticalAlignment = VerticalAlignment.Center;
                    //gridHolo.ShowGridLines = true;

                    for (int i = 0; i < 3; i++)
                    {

                        gridHolo.RowDefinitions.Add(new RowDefinition());
                        gridHolo.ColumnDefinitions.Add(new ColumnDefinition());
                    }

                    for (int i = 0; i < listMedia.Count; i++)
                    {
                        Grid.SetRow(listMedia[tableauPosition[i, 0]], tableauPosition[i, 1]);
                        Grid.SetColumn(listMedia[tableauPosition[i, 0]], tableauPosition[i, 2]);
                        gridHolo.Children.Add(listMedia[tableauPosition[i, 0]]);
                    }
                }
                else
                {
                    MessageBox.Show("Probleme sur l'extension du fichier selectionné", "Message d'erreur", MessageBoxButton.OK, MessageBoxImage.Error);
                    Close();
                }
            }
            else if (path != null)
            {
                List<Grid> listgrid = new List<Grid>();
                listgrid = creatGrid();

                int[,] tableauPosition = new int[4, 3]
                                        {{0, 0, 1},
                                         {1,  1, 2},
                                         {2,  2, 1},
                                         {3,  1, 0}};

                gridHolo.HorizontalAlignment = HorizontalAlignment.Center;
                gridHolo.VerticalAlignment = VerticalAlignment.Center;
                //gridHolo.ShowGridLines = true;

                for (int i = 0; i < 3; i++)
                {

                    gridHolo.RowDefinitions.Add(new RowDefinition());
                    gridHolo.ColumnDefinitions.Add(new ColumnDefinition());
                }

                for (int i = 0; i < listgrid.Count; i++)
                {
                    Grid.SetRow(listgrid[tableauPosition[i, 0]], tableauPosition[i, 1]);
                    Grid.SetColumn(listgrid[tableauPosition[i, 0]], tableauPosition[i, 2]);
                    gridHolo.Children.Add(listgrid[tableauPosition[i, 0]]);
                }
            }
            else
            {
                MessageBox.Show("Probleme sur l'élement selectionné", "Message d'erreur", MessageBoxButton.OK, MessageBoxImage.Error);
                Close();
            }

        }

        public List<Image> creationImag()
        {
            List<Image> images = new List<Image>();

            images.Add(initImage(0));
            images.Add(initImage(90));
            images.Add(initImage(180));
            images.Add(initImage(270));

            return images;
        }

        public Image initImage(int rotate)
        {
            string path = proc.SelectByName(nameComboBox.Text);
            Image image = new Image();
            image.Width = WIDTH;
            image.Height = HEIGHT;
            image.Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
            //image.Margin = new Thickness(marginLeft, marginTop, marginRight, marginBottom);

            RotateTransform transform = new RotateTransform(rotate);
            image.RenderTransformOrigin = new Point(0.5, 0.5);
            image.RenderTransform = transform;

            return image;
        }

        public List<MediaElement> creatMedia()
        {
            List<MediaElement> medias = new List<MediaElement>();


            medias.Add(initMedia(0));
            medias.Add(initMedia(90));
            medias.Add(initMedia(180));
            medias.Add(initMedia(270));

            return medias;
        }

        public MediaElement initMedia(int rotate)
        {
            string path = proc.SelectByName(nameComboBox.Text);
            MediaElement media = new MediaElement();
            media.Height = HEIGHT;
            media.Width = WIDTH;
            media.Source = new Uri(path);
            RotateTransform transform = new RotateTransform(rotate);
            media.RenderTransformOrigin = new Point(0.5, 0.5);
            media.RenderTransform = transform;

            return media;
        }

        public List<Grid> creatGrid()
        {
            List<Grid> grids = new List<Grid>();

            grids.Add(initGrid(0));
            grids.Add(initGrid(90));
            grids.Add(initGrid(180));
            grids.Add(initGrid(270));

            return grids;
        }

        public Grid initGrid(int rotate)
        {
            string path = proc.SelectByName(nameComboBox.Text);
            TextBlock txtB = new TextBlock();
            txtB.TextWrapping = TextWrapping.Wrap;
            txtB.Text = path;
            txtB.Background = Brushes.White;
            Grid gridTxtB = new Grid();
            gridTxtB.Height = HEIGHT;
            gridTxtB.Width = WIDTH;
            RotateTransform transform = new RotateTransform(rotate);
            gridTxtB.RenderTransformOrigin = new Point(0.5, 0.5);
            gridTxtB.RenderTransform = transform;
            ScaleTransform scale = new ScaleTransform(-1, 1);
            txtB.RenderTransformOrigin = new Point(0.5, 0.5);
            txtB.RenderTransform = scale;
            gridTxtB.Children.Add(txtB);

            return gridTxtB;
        }

        private void btnQuitter_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }


    private void button_Click(object sender, RoutedEventArgs e)
        {
            affichageDonnee();
        }
    }
}

